﻿using AutoMapper;
using System.Linq;

namespace HobSoft.SoccerBet2014.Client.ModelsMapper
{
    using dm = Domain.Models;
    using dms = Domain.Models.Site;
    using dmr = Domain.Models.BfRaw;
    using vm = ViewModels;
    public class ModelMapperConfiguration : Profile
    {
        protected override void Configure()
        {
            base.Configure();
            Mapper.CreateMap<string, dm.Game.GameState>().ConvertUsing<BfStateConverter>();
            Mapper.CreateMap<dm.OddsPrice, decimal>().ConvertUsing<OddsPriceConverter>();
            Mapper.CreateMap<dm.OddsPrice.OddsPriceType, vm.BetItem>().ConvertUsing<BetToConverter>();

            Mapper.CreateMap<dm.BFEvent, vm.Game>()
                .ForMember(vg => vg.State, dbe => dbe.Ignore())
                .ForMember(vg => vg.BfEventID, dbe => dbe.MapFrom(be => be.EventID))
                .ForMember(vg => vg.HomeTeam, dbe => dbe.MapFrom(be => be.HomeTeam))
                .ForMember(vg => vg.AwayTeam, dbe => dbe.MapFrom(be => be.AwayTeam))
                .ForMember(vg => vg.HomeScore, dbe => dbe.MapFrom(be => be.HomeScore))
                .ForMember(vg => vg.AwayScore, dbe => dbe.MapFrom(be => be.AwayScore))
                .ForMember(vg => vg.StartTime, dbe => dbe.MapFrom(be => be.StartTime))
                .ForMember(vg => vg.League, dbe => dbe.MapFrom(be => be.Tournament))
                .ForMember(vg => vg.TimeState, dbe => dbe.MapFrom(be => be.DisplayTime))
                .ForMember(vg => vg.GameState, dbe => dbe.MapFrom(be => be.State));

            Mapper.CreateMap<dm.Game, vm.Game>()
                .ForMember(vg => vg.State, dbe => dbe.Ignore())
                .ForMember(vg => vg.HomeTeam, dmg => dmg.MapFrom(dg => dg.HomeTeam))
                .ForMember(vg => vg.HomeScore, dmg => dmg.MapFrom(dg => dg.HomeScore))
                .ForMember(vg => vg.HomeRed, dmg => dmg.MapFrom(dg => dg.HomeRed))
                .ForMember(vg => vg.AwayTeam, dmg => dmg.MapFrom(dg => dg.AwayTeam))
                .ForMember(vg => vg.AwayScore, dmg => dmg.MapFrom(dg => dg.AwayScore))
                .ForMember(vg => vg.AwayRed, dmg => dmg.MapFrom(dg => dg.AwayRed))
                .ForMember(vg => vg.StartTime, dmg => dmg.MapFrom(dg => dg.StartTime))
                .ForMember(vg => vg.League, dmg => dmg.MapFrom(dg => dg.Tournament))
                .ForMember(vg => vg.TimeState, dmg => dmg.MapFrom(dg => dg.PlayeTime))
                .ForMember(vg => vg.GameState, dmg => dmg.MapFrom(dg => dg.State));

            Mapper.CreateMap<dm.Odds, vm.Odds>()
                .ForMember(vo => vo.OddsID, dmo => dmo.MapFrom(dos => dos.OddsID))
                .ForMember(vo => vo.BmID, dmo => dmo.MapFrom(dos => dos.BmID))
                .ForMember(vo => vo.GameID, dmo => dmo.MapFrom(dos => dos.GameID))
                .ForMember(vo => vo.Type, dmo => dmo.MapFrom(dos => dos.Type));


            Mapper.CreateMap<dm.BFEvent, vm.BfEvent>()
                .ForMember(ve => ve.State, dbe => dbe.Ignore())
                .ForMember(ve => ve.MarketModels, dbe => dbe.Ignore())
                .ForMember(ve => ve.EventID, dbe => dbe.MapFrom(be => be.EventID))
                .ForMember(ve => ve.HomeTeam, dbe => dbe.MapFrom(be => be.HomeTeam))
                .ForMember(ve => ve.AwayTeam, dbe => dbe.MapFrom(be => be.AwayTeam))
                .ForMember(ve => ve.HomeScore, dbe => dbe.MapFrom(be => be.HomeScore))
                .ForMember(ve => ve.AwayScore, dbe => dbe.MapFrom(be => be.AwayScore))
                .ForMember(ve => ve.Path, dbe => dbe.MapFrom(be => be.EventPath))
                .ForMember(ve => ve.StartTime, dbe => dbe.MapFrom(be => be.StartTime))
                .ForMember(ve => ve.GameState, dbe => dbe.MapFrom(be => be.State))
                .ForMember(ve => ve.TimeState, dbe => dbe.MapFrom(be => be.DisplayTime));
                //.ForMember(ve => ve.MarketModels, dbe => dbe.MapFrom(be => be.Markets));

            Mapper.CreateMap<dm.BFMarket, vm.BfMarket>()
                .ForMember(vm => vm.State, dbm => dbm.Ignore())
                .ForMember(vm => vm.IsFilter, dbm => dbm.Ignore())
                .ForMember(vm => vm.Event, dbm => dbm.Ignore())
                .ForMember(vm => vm.MarketID, dbm => dbm.MapFrom(m => m.MarketID))
                .ForMember(vm => vm.EventID, dbm => dbm.MapFrom(m => m.EventID))
                .ForMember(vm => vm.MarketCategory, dbm => dbm.MapFrom(m => m.CategoryID))
                .ForMember(vm => vm.MarketName, dbm => dbm.MapFrom(m => m.MarketName))
                .ForMember(vm => vm.MarketStatus, dbm => dbm.MapFrom(m => m.State))
                .ForMember(vm => vm.Runners, dbm => dbm.MapFrom(m => m.Runners));

            Mapper.CreateMap<dmr.MarketBook, vm.BfMarket>()
                .ForMember(vm => vm.State, dbm => dbm.Ignore())
                .ForMember(vm => vm.MarketStatus, dbm => dbm.MapFrom(m => m.Status))
                .ForMember(vm => vm.IsFilter, dbm => dbm.Ignore())
                .ForMember(vm => vm.Runners, dbm => dbm.Ignore());

            Mapper.CreateMap<dmr.Runner, vm.Runner>()
                .ForMember(vm => vm.State, dbm => dbm.Ignore())
                .ForMember(vm => vm.IsFilter, dbr => dbr.Ignore())
                .ForMember(vm => vm.RunnerName, dbr => dbr.Ignore())
                .ForMember(vm => vm.Handicap, dbr => dbr.MapFrom(m => m.Handicap))
                .ForMember(vm => vm.LastPrice, dbr => dbr.MapFrom(m => m.LastPriceTraded))
                .ForMember(vm => vm.TotalMatched, dbr => dbr.MapFrom(m => m.TotalMatched))
                .ForMember(vm => vm.SelectionID, dbr => dbr.MapFrom(m => m.SelectionId))
                .ForMember(vm => vm.LA1, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(la => la.Size).ElementAtOrDefault(0)))
                .ForMember(vm => vm.LA2, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(la => la.Size).ElementAtOrDefault(1)))
                .ForMember(vm => vm.LA3, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(la => la.Size).ElementAtOrDefault(2)))
                .ForMember(vm => vm.LP1, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(lp => lp.Price).ElementAtOrDefault(0)))
                .ForMember(vm => vm.LP2, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(lp => lp.Price).ElementAtOrDefault(1)))
                .ForMember(vm => vm.LP3, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToLay.Select(lp => lp.Price).ElementAtOrDefault(2)))
                .ForMember(vm => vm.BA1, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(ba => ba.Size).ElementAtOrDefault(0)))
                .ForMember(vm => vm.BA2, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(ba => ba.Size).ElementAtOrDefault(1)))
                .ForMember(vm => vm.BA3, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(ba => ba.Size).ElementAtOrDefault(2)))
                .ForMember(vm => vm.BP1, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(bp => bp.Price).ElementAtOrDefault(0)))
                .ForMember(vm => vm.BP2, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(bp => bp.Price).ElementAtOrDefault(1)))
                .ForMember(vm => vm.BP3, dbr => dbr.MapFrom(m => m.ExchangePrices.AvailableToBack.Select(bp => bp.Price).ElementAtOrDefault(2)))
                ;

            Mapper.CreateMap<dm.BFRunner, vm.Runner>()
                .ForMember(vr => vr.State, dbr => dbr.Ignore())
                .ForMember(vr => vr.IsFilter, dbr => dbr.Ignore())
                .ForMember(vr => vr.BA1, dmr => dmr.MapFrom(r => r.BA1))
                .ForMember(vr => vr.BA2, dmr => dmr.MapFrom(r => r.BA2))
                .ForMember(vr => vr.BA3, dmr => dmr.MapFrom(r => r.BA3))
                .ForMember(vr => vr.BP1, dmr => dmr.MapFrom(r => r.BP1))
                .ForMember(vr => vr.BP2, dmr => dmr.MapFrom(r => r.BP2))
                .ForMember(vr => vr.BP3, dmr => dmr.MapFrom(r => r.BP3))
                .ForMember(vr => vr.LA1, dmr => dmr.MapFrom(r => r.LA1))
                .ForMember(vr => vr.LA2, dmr => dmr.MapFrom(r => r.LA2))
                .ForMember(vr => vr.LA3, dmr => dmr.MapFrom(r => r.LA3))
                .ForMember(vr => vr.LP1, dmr => dmr.MapFrom(r => r.LP1))
                .ForMember(vr => vr.LP2, dmr => dmr.MapFrom(r => r.LP2))
                .ForMember(vr => vr.LP3, dmr => dmr.MapFrom(r => r.LP3))
                .ForMember(vr => vr.MarketID, dmr => dmr.MapFrom(r => r.MarketID))
                .ForMember(vr => vr.SelectionID, dmr => dmr.MapFrom(r => r.SelectionID))
                .ForMember(vr => vr.RunnerName, dmr => dmr.MapFrom(r => r.RunnerName))
                .ForMember(vr => vr.Handicap, dmr => dmr.MapFrom(r => r.Handicap))
                .ForMember(vr => vr.AsianLineId, dmr => dmr.MapFrom(r => r.AsianLineId))
                .ForMember(vr => vr.LastPrice, dmr => dmr.MapFrom(r => r.LastPrice))
                .ForMember(vr => vr.OrderIndex, dmr => dmr.MapFrom(r => r.OrderIndex))
                .ForMember(vr => vr.TotalMatched, dmr => dmr.MapFrom(r => r.TotalMatched));

            Mapper.CreateMap<dm.Bookmaker, vm.Bookmaker>();
            Mapper.CreateMap<dm.BmAccount, vm.BmAccount>();
            Mapper.CreateMap<vm.Bookmaker, dm.Bookmaker>();
            Mapper.CreateMap<vm.BmAccount, dm.BmAccount>();
            Mapper.CreateMap<dm.BFAccount, vm.BfAccount>();
            Mapper.CreateMap<vm.BfAccount, dm.BFAccount>();

            Mapper.CreateMap<dms.BettingOrder, vm.BmOrder>()
                .ForMember(vo => vo.BmID, dom => dom.MapFrom(dod => dod.BmID))
                .ForMember(vo => vo.BetAccount, dom => dom.MapFrom(dod => dod.Account))
                .ForMember(vo => vo.RecID, dom => dom.MapFrom(dod => dod.RecID))
                .ForMember(vo => vo.League, dom => dom.MapFrom(dod => dod.RawLeagueName))
                .ForMember(vo => vo.HomeTeam, dom => dom.MapFrom(dod => dod.RawHomeTeam))
                .ForMember(vo => vo.AwayTeam, dom => dom.MapFrom(dod => dod.RawAwayTeam))
                .ForMember(vo => vo.HomeScore, dom => dom.MapFrom(dod => dod.RawHomeScore))
                .ForMember(vo => vo.AwayScore, dom => dom.MapFrom(dod => dod.RawAwayScore))
                .ForMember(vo => vo.BetTime, dom => dom.MapFrom(dod => dod.BettingTime))
                .ForMember(vo => vo.RawResult, dom => dom.MapFrom(dod => dod.RawStatus))
                .ForMember(vo => vo.BetMoney, dom => dom.MapFrom(dod => dod.Money))
                .ForMember(vo => vo.BetTo, dom => dom.MapFrom(dod => dod.PriceType))
                .ForMember(vo => vo.Handicap, dom => dom.MapFrom(dod => dod.Handicap))
                .ForMember(vo => vo.Type, dom => dom.MapFrom(dod => dod.Type));

            Mapper.CreateMap<dm.BetTick, vm.BetInfo>()
                .ForMember(vb => vb.BmID, dbm => dbm.MapFrom(db => db.BmID))
                .ForMember(vb => vb.GameID, dbm => dbm.MapFrom(db => db.GameID))
                .ForMember(vb => vb.HomeTeam, dbm => dbm.MapFrom(db => db.HomeTeam))
                .ForMember(vb => vb.AwayTeam, dbm => dbm.MapFrom(db => db.AwayTeam))
                .ForMember(vb => vb.HomeScore, dbm => dbm.MapFrom(db => db.HomeScore))
                .ForMember(vb => vb.AwayScore, dbm => dbm.MapFrom(db => db.AwayScore))
                .ForMember(vb => vb.OddsValue, dbm => dbm.MapFrom(db => db.Price))
                .ForMember(vb => vb.Handicap, dbm => dbm.MapFrom(db => db.Handicap))
                .ForMember(vb => vb.MaxBet, dbm => dbm.MapFrom(db => db.MaxAmount))
                .ForMember(vb => vb.MinBet, dbm => dbm.MapFrom(db => db.MinAmount))
                .ForMember(vb => vb.Type, dbm => dbm.MapFrom(db => db.MarketType))
                ;


            Mapper.CreateMap<dmr.CurrentOrderSummary, vm.BfOrder>()
                .ForMember(vo => vo.State, dom => dom.Ignore())
                .ForMember(vo => vo.BetID, dom => dom.MapFrom(dod => dod.BetId))
                .ForMember(vo => vo.SelectionID, dom => dom.MapFrom(dod => dod.SelectionId))
                .ForMember(vo => vo.MarketID, dom => dom.MapFrom(dod => dod.MarketId))
                .ForMember(vo => vo.Price, dom => dom.MapFrom(dod => dod.PriceSize.Price))
                .ForMember(vo => vo.Amount, dom => dom.MapFrom(dod => dod.PriceSize.Size))
                .ForMember(vo => vo.OrderAmount, dom => dom.MapFrom(dod => dod.SizeRemaining))
                .ForMember(vo => vo.OrderTurnover, dom => dom.MapFrom(dod => dod.SizeLapsed))
                .ForMember(vo => vo.SubmitTime, dom => dom.MapFrom(dod => dod.PlacedDate))
                .ForMember(vo => vo.OrderState, dom => dom.MapFrom(dod => dod.Status))
                ;

        }

        public override string ProfileName
        {
            get
            {
                return this.GetType().FullName;
            }
        }
    }
}
